package com.example.edianzu.somur.utils.view;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * author：风不会停息 on 2017/8/3 13:41
 * mailbox：yh131412hys@163.com
 * project：AdvancedUI
 * tel：13295355900
 * describe：
 */

public class HeaderAndFooterRecyclerAdapter extends RecyclerView.Adapter{

    ArrayList<View> mHeaderViewInfos ;
    ArrayList<View> mFooterViewInfos;
    private RecyclerView.Adapter mAdapter;

    public HeaderAndFooterRecyclerAdapter(ArrayList<View> headerViewInfos, ArrayList<View> footerViewInfos, RecyclerView.Adapter adapter) {

        mAdapter = adapter;

        if (headerViewInfos == null) {
            mHeaderViewInfos = new ArrayList<>();
        } else {
            mHeaderViewInfos = headerViewInfos;
        }

        if (footerViewInfos == null) {
            mFooterViewInfos = new ArrayList<>();
        } else {
            mFooterViewInfos = footerViewInfos;
        }
    }

    @Override
    public int getItemCount() {
        if (mAdapter != null) {
            return getFootersCount() + getHeadersCount() + mAdapter.getItemCount();
        } else {
            return getFootersCount() + getHeadersCount();
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//也要划分三个区域
        // Header (negative positions will throw an IndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (position < numHeaders) {
            return ;
        }

        // Adapter
        int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getItemCount();
            if (adjPosition < adapterCount) {
                mAdapter.onBindViewHolder(holder,adjPosition);
                return ;
            }
        }
        // Footer (off-limits positions will throw an IndexOutOfBoundsException)
    }

//    判断当前条目是什么类型的--决定渲染什么视图什么数据
    @Override
    public int getItemViewType(int position) {

        // Header (negative positions will throw an IndexOutOfBoundsException)
        int numHeaders = getHeadersCount();
        if (position < numHeaders) {
            return RecyclerView.INVALID_TYPE;
        }

        // Adapter
        int adjPosition = position - numHeaders;
        int adapterCount = 0;
        if (mAdapter != null) {
            adapterCount = mAdapter.getItemCount();
            if (adjPosition < adapterCount) {
                return mAdapter.getItemViewType(adjPosition);
            }
        }

        // Footer (off-limits positions will throw an IndexOutOfBoundsException)
        return RecyclerView.INVALID_TYPE-1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType== RecyclerView.INVALID_TYPE) {
            return new HeaderViewHolder(mHeaderViewInfos.get(0));
        }else if (viewType==RecyclerView.INVALID_TYPE-1){
            return new HeaderViewHolder(mFooterViewInfos.get(0));
        }

        return mAdapter.onCreateViewHolder(parent,viewType);
    }

    private int getFootersCount() {
        return mFooterViewInfos.size();
    }

    private int getHeadersCount() {
        return mHeaderViewInfos.size();
    }

    private static class HeaderViewHolder extends RecyclerView.ViewHolder{

        public HeaderViewHolder(View itemView) {
            super(itemView);
        }
    }
}
